function swmat=countswitch(st,svec);
%Count the number of switches the occured in the vector st 
%for the states numbered in svec 
%st  (tx1) vector generated according to a 1 st order Markov 
%svec must be a row or column vector of length ns numbering the states 
%swmat is (nsxns) 
%the i-th column contains nij=number of transitions for ( S(t)=j| S(t-1)=i ) 
%j=1,....,ns 
%Note: only t-1 switches are recorded 
ns=length(svec);
t=length(st);
swmat=zeros(ns,ns); 
ii=1; 
for ii=2:t 
    s0=st(ii-1); 
    s1=st(ii); 
    swmat(s1,s0)=swmat(s1,s0) + 1; 
end 